﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.Wave;

namespace CallCenter.Forms
{
    public partial class FrmIncoming : Form
    {
        /// <summary>
        /// 呼入电话号码
        /// </summary>
        private string teleNumber;

        /// <summary>
        /// 自动关闭时长
        /// </summary>
        private int autoCloseSeconds;

        /// <summary>
        /// MP3 播放
        /// </summary>
        private AudioFileReader audioFileReader;
        private IWavePlayer waveOut;

        // MP3 播放初始化是否完成
        private bool isInitializeAudio;

        public FrmIncoming(string teleNumber)
        {
            InitializeComponent();
            this.teleNumber = teleNumber;

            autoCloseSeconds = 8;
            isInitializeAudio = false;
        }

        //private void btnAnswer_Click(object sender, EventArgs e)
        //{
        //    // 接听
        //    this.DialogResult = DialogResult.OK;
        //    this.Close();
        //}

        private void btnHangup_Click(object sender, EventArgs e)
        {
            // 挂断
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void timerClose_Tick(object sender, EventArgs e)
        {
            timerClose.Stop();
            autoCloseSeconds--;

            if (this.Created)
            {
                this.BeginInvoke((MethodInvoker)delegate
                {
                    lblSeconds.Text = $"{autoCloseSeconds} s";
                });
            }

            if (autoCloseSeconds <= 0)
            {
                Stop();
                this.DialogResult = DialogResult.OK;
                this.Close();
                return;
            }

            timerClose.Start();
        }

        private void FrmIncoming_Load(object sender, EventArgs e)
        {
            lblMsg.Text = "电话 " + teleNumber + " 呼入，请使用话机接听或直接挂断。";

            InitializeAudio();
            Play();
        }

        /// <summary>
        /// 初始化播放器
        /// </summary>
        private void InitializeAudio()
        {
            try
            {
                // 初始化音频播放设备
                waveOut = new WaveOutEvent();
                // 加载MP3文件
                audioFileReader = new AudioFileReader(@"ivr.mp3");
                // 设置音频输出设备
                waveOut.Init(audioFileReader);

                isInitializeAudio = true;
            }
            catch(Exception ex)
            {
                log4net.LogManager.GetLogger(typeof(FrmIncoming)).Error($"异常：{ex.Message}");
            }
        }

        private void Play()
        {
            if (isInitializeAudio)
            {
                if (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    waveOut.Stop();
                }

                waveOut.Play();
            }
        }

        private void Stop()
        {
            if (isInitializeAudio)
            {
                if (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    waveOut.Stop();
                }
            }
        }

        private void FrmIncoming_FormClosing(object sender, FormClosingEventArgs e)
        {
            Stop();
        }
    }
}
